import { Controller, Post, Body, BadRequestException } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UsersService } from './users.service';

@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post()
  async createUser(@Body() createUserDto: CreateUserDto) {
    // 查找用户
    const user = await this.usersService.findOne(createUserDto.account);
    // 判断用户是否注册
    if (user) {
      throw new BadRequestException('请使用此账号登录', '用户已经存在');
    }
    // 创建用户
    return await this.usersService.createOne(createUserDto);
  }
}
